/**
 * 单例函数生成器
 */


/**
 * 单例函数生成器
 * */
const singleFunCreate = (promiseFun) => {
  return function () {
    let loading = false
    let promiseCache = []
    return function () {
      return new Promise((resolve, reject) => {
        promiseCache.push({
          resolve,
          reject
        })
        if (!loading) {
          loading = true
          promiseFun().then(res => {
            while (promiseCache.length) {
              promiseCache.shift().resolve(res)
            }
            loading = false
          }).catch(err => {
            while (promiseCache.length) {
              promiseCache.shift().reject(err)
            }
            loading = false
          })
        }
      })
    }
  }
}
export default singleFunCreate